package com.flower.subject.application.controller;

import com.alibaba.excel.EasyExcel;
import com.flower.subject.application.config.DemoDataListener;
import com.flower.subject.infra.basic.dao.StudentDao;
import com.flower.subject.infra.basic.entity.Student;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.oned.Code128Writer;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.List;



/**
 * @author lavender
 * @version 1.0
 * @date 2024-09-04 13:30
 */
@RestController
@RequestMapping("/source")
public class MultiSourceController {


    @Resource
    private StudentDao studentDao;


    @GetMapping("/list")
    private List<Student> list(){
        List<Student> query = studentDao.query();
        return query;
    }


    @GetMapping("/download")
    private void upload(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");

       String fileName = "simpleWrite" + System.currentTimeMillis() ;
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        List<Student> query = studentDao.query();
        EasyExcel.write(response.getOutputStream(), Student.class).sheet().doWrite(query);
    }



    /**
     * 文件上传
     * <p>1. 创建excel对应的实体对象 参照{@link Student}
     * <p>2. 由于默认一行行的读取excel，所以需要创建excel一行一行的回调监听器，参照{@link DemoDataListener}
     * <p>3. 直接读即可
     */
    @PostMapping("upload")
    @ResponseBody
    public String upload(MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(), Student.class, new DemoDataListener(studentDao)).sheet().doRead();
        return "success";
    }


    @GetMapping("/generate-barcode")
    public String generateBarcode(@RequestParam String content, @RequestParam String path, @RequestParam int width, @RequestParam int height) {
        this.generateBarcodeImage(content, path, width, height);
        return "Barcode generated successfully at " + path;
    }


    /**
     * 生成条形码并保存到指定路径
     * @param content 条形码内容
     * @param path 保存路径
     * @param width 条形码宽度
     * @param height 条形码高度
     */
    public void generateBarcodeImage(String content, String path, int width, int height) {
        Code128Writer barcodeWriter = new Code128Writer();
        BitMatrix bitMatrix = null;
        try {
            bitMatrix = barcodeWriter.encode(content, BarcodeFormat.CODE_128, width, height);
            long l = System.currentTimeMillis();
            Path filePath = FileSystems.getDefault().getPath("D:\\emos\\image" +l + ".png");
            MatrixToImageWriter.writeToPath(bitMatrix, "PNG", filePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
